package com.factory.iamp.pojo;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.RandomStringUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String uid;
    private String username;
    private String password;
    private String realname;
    private String avatar;
    private int role;
    private int line;
    private String sign_img;
    private String token;

    public boolean isAdmin() {
        return role == 1 || role == 2;
    }

    public String makeToken() {
        return JWT.create().withClaim("uid", this.uid).
                withClaim("token", this.token).sign(Algorithm.HMAC256(this.password));
    }

    public String renewToken() {
        this.token = RandomStringUtils.randomAlphanumeric(16);
        return this.token;
    }

    public String hashPassword() {
        return JWT.create().withAudience(this.username).sign(Algorithm.HMAC256(this.password));
    }
}